package com.zyht.common.web.listener;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 测试信息
 * 
 * @author dingyaming
 */
public class ServerInfo {

	private static final Logger logger = LoggerFactory.getLogger(ServerInfo.class);

	public static void main(String[] args) throws Exception{

		systeminfo();
		networkinfo();
	}

	/**
	 * @throws Exception
	 * 
	 */
	public static void systeminfo() throws Exception{

		if(logger.isDebugEnabled()){
			logger.debug(">>systeminfo-------------------------------------------");
		}
		Map<String, String> map = System.getenv();
		Set<Entry<String, String>> sm = map.entrySet();
		Iterator<Entry<String, String>> t = sm.iterator();
		while (t.hasNext()){
			Entry<String, String> entry = t.next();
			if(logger.isDebugEnabled()){
				logger.debug(entry.getKey() + "=" + entry.getValue());
			}
		}
		if(logger.isDebugEnabled()){
			logger.debug("-------------------------------------------");
		}

		Properties properties = System.getProperties();
		Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();
		while (it.hasNext()){
			Entry<Object, Object> entry = it.next();
			logger.debug(entry.getKey() + "=" + entry.getValue());
		}
		if(logger.isDebugEnabled()){
			logger.debug("-------------------------------------------");
		}
	}

	public static void networkinfo() throws Exception{

		InetAddress addr = InetAddress.getLocalHost();
		if(logger.isDebugEnabled()){
			logger.debug(">>networkinfo-------------------------------------------");
			logger.debug(addr.toString());
			logger.debug(addr.getHostAddress());// 获得本机IP
			logger.debug(addr.getHostName());// 获得本机名称
		}

		Enumeration<NetworkInterface> ni = NetworkInterface.getNetworkInterfaces();
		List<String> macL = new ArrayList<String>();
		List<String> hostAddressL = new ArrayList<String>();
		List<String> hostNameL = new ArrayList<String>();
		String l = "-";
		while (ni.hasMoreElements()){
			NetworkInterface netI = ni.nextElement();
			if(netI != null && netI.isUp()){
				byte[] bytes = netI.getHardwareAddress();
				if(bytes != null && bytes.length == 6){
					StringBuilder sBuild = new StringBuilder();
					for(byte b:bytes){
						// 与11110000作按位与运算以便读取当前字节高4位
						sBuild.append(Integer.toHexString((b & 240) >> 4));
						// 与00001111作按位与运算以便读取当前字节低4位
						sBuild.append(Integer.toHexString(b & 15));
						sBuild.append(l);
					}
					sBuild.deleteCharAt(sBuild.length() - 1);
					macL.add(sBuild.toString().toUpperCase());
				}
			}
			Enumeration<InetAddress> addresses = netI.getInetAddresses();
			while (addresses.hasMoreElements()){
				InetAddress ip = addresses.nextElement();
				if(ip != null && ip instanceof Inet4Address){
					hostAddressL.add(ip.getHostAddress());
					hostNameL.add(ip.getHostName());
				}
			}
		}
		if(logger.isDebugEnabled()){
			logger.debug("ServerMAC=" + macL.toString());
			logger.debug("ServerHostAddress=" + hostAddressL.toString());
			logger.debug("ServerHostName=" + hostNameL.toString());
			logger.debug("-------------------------------------------");
		}
	}
}
